<template>
    <div v-draggable class="legend-com" :style="styleLocation">
        <div class="header">
            <div class="left">产品图层</div>
            <div class="right">
                <div :class="['eye', disabled ? 'disabled' : '']" @click="showAll" title="全部显隐"></div>
                <div class="close" @click="closeAll" title="全部关闭"></div>
            </div>
        </div>

        <template v-for="(el, index) in option" :key="index">
            <div v-if="el.show" class="item">
                <div class="item-header">
                    <div class="title">{{ el.title }}</div>
                    <div class="btns">
                        <div :class="['eye', el.disabled ? 'disabled' : '']" @click="show(el)" title="图层显隐"></div>
                        <div class="close" @click="close(el)" title="图层关闭"></div>
                    </div>
                </div>
                <div class="content">
                    <div class="line-item">
                        <div class="left"></div>
                        <div class="line"></div>
                        <div class="right"></div>
                    </div>
                    <div class="color-wrap" v-for="item in el.options" :key="item.color">
                        <div class="color" :style="{ background: item.imageData.startsWith('#') ? item.imageData : `rgba(${item.imageData})` }"></div>
                        <div class="label">{{ item.label }}</div>
                    </div>
                </div>
            </div>
        </template>
    </div>
</template>

<script lang='ts' setup>
import { ref, computed, watch } from 'vue';

const props = defineProps({
    data: {
        type: Array,
        default: () => ([])
    },
    top: { //距离top
        type: Number,
        default: 0
    },
    right: { //距离right
        type: Number,
        default: 0
    },
    bottom: { //距离bottom
        type: Number,
        default: 0
    },
    left: { //距离left
        type: Number,
        default: 0
    },
    zIndex: {
        type: Number,
        default: 100
    },
    width: { //组件的宽度
        type: Number,
        default: 0
    },
    drag: {
        type: Boolean,
        default: true
    }
});

type ObjWithUnknownAttributes = {
    [key: string]: string;
};

const styleLocation = computed(() => {
    let obj: ObjWithUnknownAttributes = {};
    if (props.top) obj.top = `${props.top}px`;
    if (props.right) obj.right = `${props.right}px`;
    if (props.bottom) obj.bottom = `${props.bottom}px`;
    if (props.left) obj.left = `${props.left}px`;
    if (props.zIndex) obj['z-index'] = `${props.zIndex}`;
    if (props.width) {
        obj.width = `${props.width}px`;
    } else {
        obj.width = 'calc(100% - 1000px)';
    }
    return obj;
});

const option = ref<any[]>([]);
watch(() => props.data, (newVal, oldVal) => {
    option.value = newVal.map((el: any) => ({
        ...el,
        options: el.options.length > 20 ? el.options.map(({ label, ...o }: any, index: number) => ({
            ...o,
            label: index % 2 === 0 ? '' : label,
        })) : el.options
    }));
}, {
	deep: true,
    immediate: true
});

const disabled = ref(false);
const emit = defineEmits(['showAll', 'closeAll', 'show', 'close']);
const showAll = () => {
    disabled.value = !disabled.value;
        emit('showAll', !disabled.value);
}
const closeAll = () => {
    emit('closeAll');
}
const show = (el: any) => {
    el.disabled = !el.disabled;
    emit('showAll', !el.disabled);
}
const close = (el: any) => {
    el.show = !el.show;
    emit('closeAll');
}
</script>

<style lang='scss' scoped>
.legend-com {
    position: fixed !important;
    padding-bottom: 12px;
    background-color: rgba(241, 241, 241, 0.92);
    box-shadow: rgba(0, 0, 0, 0.3) 0px 4px 9px;

    .header {
        display: flex;
        justify-content: space-between;
        border-top: 2px solid #4D98E4;
        margin-bottom: 5px;
        width: 100%;
        height: 35px;
        line-height: 25px;
        font-size: 15px;
        color: #fff;
        background-color: rgba(241, 241, 241, 0.92);
        background-image: url('');
        background-repeat: no-repeat;

        .left {
            padding-left: 10px;
            font-size: 15px;
            font-weight: 700;
        }

        .right {
            display: flex;
            justify-content: flex-end;
            align-items: center;
            margin-right: 15px;
            width: 100px;

            .eye {
                margin: 0 5px;
                width: 14px;
                height: 14px;
                background-image: url('');
                background-repeat: no-repeat;
                cursor: pointer;
            }

            .disabled {
                background-image: url('');
            }

            .close {
                margin: 0 5px;
                width: 14px;
                height: 14px;
                background-image: url('');
                background-repeat: no-repeat;
                cursor: pointer;
            }
        }
    }

    .item {
        .item-header {
            display: flex;
            justify-content: space-between;
            align-items: center;
            box-sizing: border-box;
            padding: 5px 15px;
            font-size: 12px;
            color: #ffffff;
            background-color: #4D98E4;

            .title {
                font-size: 14px;
            }

            .btns {
                display: flex;
                justify-content: flex-end;
                align-items: center;
                width: 100px;

                .eye {
                    margin: 0 5px;
                    width: 14px;
                    height: 14px;
                    background-image: url('');
                    background-repeat: no-repeat;
                    cursor: pointer;
                }

                .disabled {
                    background-image: url('');
                }

                .close {
                    margin: 0 5px;
                    width: 14px;
                    height: 14px;
                    background-image: url('');
                    background-repeat: no-repeat;
                    cursor: pointer;
                }
            }
        }

        .content {
            position: relative;
            display: flex;
            box-sizing: border-box;
            padding: 0 15px;
            width: 100%;
            font-size: 12px;
            background-color: #4D98E4;

            .line-item {
                position: absolute;
                box-sizing: border-box;
                width: calc(100% - 30px);
                height: 14px;

                .line {
                    position: absolute;
                    top: 5px;
                    box-sizing: border-box;
                    margin-top: 2px;
                    width: 100%;
                    height: 1px;
                    background-color: #ffffff;
                }

                .left,
                .right {
                    top: 1px;
                    box-sizing: border-box;
                    border: 1px solid rgb(204, 204, 204);
                    border-radius: 2px;
                    width: 8px;
                    height: 12px;
                    background: rgb(238, 244, 253);
                    box-shadow: rgba(153, 153, 153, 0.709804) 0px 0px 1px 0px;
                    cursor: pointer;
                }

                .left {
                    position: absolute;
                    left: 0;
                }

                .right {
                    position: absolute;
                    right: 0;
                }
            }

            .color-wrap {
                flex: 1;
                display: flex;
                flex-direction: column;
                justify-content: flex-start;
                color: #fff;

                .color {
                    height: 14px;
                }
            }

            .color-wrap {
                flex: 1;
                display: flex;
                flex-direction: column;
                justify-content: flex-start;
                color: #fff;

                .color {
                    height: 14px;
                }
            }
        }
    }
}
</style>